package com.example.shiyun.Util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.example.shiyun.Pojo.Pojo_city;
import com.example.shiyun.Pojo.Pojo_citydata;
import com.example.shiyun.R;
import com.google.gson.Gson;

import org.litepal.LitePal;

import java.util.List;

/**
 * 天气app的工具类
 */
public class Util {
    public static void Setimmersion(Window win)
    {
        //设置沉浸模式
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = win.getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            win.setStatusBarColor(Color.TRANSPARENT);
//            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
            //设置底部栏透明
//            win.setNavigationBarColor(Color.TRANSPARENT);
        }
    }
    //检查网络状态
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
    //显示toast
    public static void showmeg(final String text, final Activity activity)
    {
       activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
            }
        });
    }

    //退出应用程序
    public static void exitApp(Context context){
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();
        for (ActivityManager.AppTask appTask : appTaskList) {
            appTask.finishAndRemoveTask();
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
    //初始化数据库中的数据
    public static void litepal_data(String data)
    {
        //删除所有数据
        int i1 = LitePal.deleteAll(Pojo_citydata.class);
        Log.d("xxnn",i1+"");
        Gson gson=new Gson();
        Pojo_city pojo_city = gson.fromJson(data, Pojo_city.class);
        for (int i = 0; i < 15; i++) {
            Pojo_citydata citydata=new Pojo_citydata();
            citydata.setCity(pojo_city.getCityInfo().getCity());
            citydata.setShidu(pojo_city.getData().getShidu());
            citydata.setQuality(pojo_city.getData().getQuality());
            citydata.setWendu(pojo_city.getData().getWendu());
            citydata.setCitykey(pojo_city.getCityInfo().getCitykey());

            citydata.setDate(pojo_city.getData().getForecast().get(i).getDate());
            citydata.setHigh(pojo_city.getData().getForecast().get(i).getHigh());
            citydata.setLow(pojo_city.getData().getForecast().get(i).getLow());
            citydata.setYmd(pojo_city.getData().getForecast().get(i).getYmd());
            citydata.setWeek(pojo_city.getData().getForecast().get(i).getWeek());
            citydata.setSunrise(pojo_city.getData().getForecast().get(i).getSunrise());
            citydata.setSunset(pojo_city.getData().getForecast().get(i).getSunset());
            citydata.setFx(pojo_city.getData().getForecast().get(i).getFx());
            citydata.setFl(pojo_city.getData().getForecast().get(i).getFl());
            citydata.setType(pojo_city.getData().getForecast().get(i).getType());
            citydata.setNotice(pojo_city.getData().getForecast().get(i).getNotice());
            citydata.save();
        }
    }
    //设置天气图
    public static int weather_chart(String theWeather)
    {
        switch (theWeather)
        {
            case "暴雪":
                return R.mipmap.baoxue;
            case "暴雨":
                return R.mipmap.baoyu;
            case "大暴雨":
                return R.mipmap.dadabaoyu;
            case "暴雨到大到暴雨":
                return R.mipmap.baoyudaodayu;
            case "大暴雨到特打暴雨":
                return R.mipmap.dabaoyudaotedabaoyu;
            case "大暴雪":
                return R.mipmap.dadabaoxue;
            case "大雾":
                return R.mipmap.dawu;
            case "大雪":
                return R.mipmap.daxue;
            case "大雨":
                return R.mipmap.dayu;
            case "冻雨":
                return R.mipmap.dongyu;
            case "浮尘":
                return R.mipmap.fucheng;
            case "雷阵雨":
                return R.mipmap.leichengyu;
            case "雷阵雨伴有冰雹":
                return R.mipmap.leichengyubingbao;
            case "霾":
                return R.mipmap.mai;
            case "浓雾":
                return R.mipmap.nowu;
            case "强浓雾":
                return R.mipmap.teqiangnowu;
            case "强沙尘暴":
                return R.mipmap.qiangshachengbao;
            case "多云":
                return R.mipmap.duoyun;
            case "晴":
                return R.mipmap.qing;
            case "沙尘暴":
                return R.mipmap.shachengbao;
            case "特大暴雨":
                return R.mipmap.tedabaoyu;
            case "雾":
                return R.mipmap.wu;
            case "小到中雪":
                return R.mipmap.xiaodaozhoxue;
            case "小到中雨":
                return R.mipmap.xiaodaozhoyu;
            case "小雪":
                return R.mipmap.xiaoxue;
            case "小雨":
                return R.mipmap.xiaoyu;
            case "严重霾":
                return R.mipmap.yanzhomai;
            case "扬沙":
                return R.mipmap.yangsha;
            case "夜间多云":
                return R.mipmap.yejianduoyun;
            case "夜晴":
                return R.mipmap.yeqing;
            case "阴":
                return R.mipmap.ying;
            case "雨夹雪":
                return R.mipmap.yujiaxue;
            case "阵雪":
                return R.mipmap.chengxue;
            case "阵雨":
                return R.mipmap.chengyu;
            case "中到大雪":
                return R.mipmap.zhodaodaxue;
            case "中度霾":
                return R.mipmap.zhodumai;
            case "中雪":
                return R.mipmap.zhoxue;
            case "中雨":
                return R.mipmap.zhoyu;
            case "重度霾":
                return R.mipmap.zhodumai;
            default:
                return R.mipmap.w;
        }
    }

}
